package tianqi.service.login.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import tianqi.Dao.index.indexDao;
import tianqi.Dao.user.UserDao;
import tianqi.entity.UserY;
import tianqi.entity.Zfb;
import tianqi.service.login.UserService;
import tianqi.utils.Md5;
import tianqi.utils.UuidUtil;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private indexDao indexDao;

	@Override
	public String chackLogin(String uname, String pwd, HttpServletRequest req, HttpServletResponse res, Model m) {
		// 入口参数检查
		if (uname == null || uname.trim().isEmpty()) {
			return "用户名不能为空";
		}
		if (pwd == null || pwd.trim().isEmpty()) {
			return "密码不能为空";
		}

		// 从业务层查询用户信息
		UserY findUserByUserName = new UserY();
		findUserByUserName.setUname(uname);
		UserY user = userDao.findUserByUserName(findUserByUserName);
		if (user == null) {
			return "用户名或者密码错误";
		}
		if (!user.getPwd().equals(Md5.saltMd5(pwd))) {
			return "用户名或密码错误";// 登录成功
		}
		req.getSession().setAttribute("user", user);
		m.addAttribute("user", user);
		m.addAttribute("list", indexDao.queryProduct());
		return "登录成功";
	}

	@Override
	public String regist(String uname, String pwd, HttpServletRequest req, Model m) {

		System.out.println("new String :" + uname);
		// 入口参数检查
		if (uname == null || uname.trim().isEmpty()) {
			return "用户名不能为空";
		}
		if (pwd == null || pwd.trim().isEmpty()) {
			return "密码不能为空";
		}
		if (!reg(uname, "email") && !reg(uname, "phone")) {
			return "邮箱或手机号输入有误！";
		}
		// 从业务层查询用户信息
		UserY findUserByUserName = new UserY();
		findUserByUserName.setUname(uname);
		UserY user = userDao.findUserByUserName(findUserByUserName);
		if (user != null) {
			return "用户已存在!";
		}
		String id = UuidUtil.get32UUID();
		System.out.println(id);
		String p = Md5.saltMd5(pwd);
		user = new UserY(id, uname, p, uname);
		userDao.saveUser(user);
		Zfb zfb = new Zfb();
		zfb.setMname(uname);
		zfb.setMpwd(pwd);
		zfb.setUid(id);
		zfb.setMonry(0.0);
		userDao.saveZFB(zfb);
		user.setMoney(0.0);
		req.getSession().setAttribute("user", user);
		m.addAttribute("user", user);
		m.addAttribute("list", indexDao.queryProduct());
		return "注册成功";
	}

	@Override
	public String blur(String uname) {
		System.out.println(uname);
		UserY findUserByUserName = new UserY();
		findUserByUserName.setUname(uname);
		findUserByUserName = userDao.findUserByUserName(findUserByUserName);
		if (findUserByUserName != null) {
			return "false";
		} else {
			return "true";
		}

	}

	@Override
	public Zfb findmbByUserName(Zfb name) {

		return userDao.findmbByUserName(name);
	}

	@Override
	public Integer updateByMN(Zfb name) {
		Integer integer = 0;
		if (userDao.updateByMN(name) > 0) {
			UserY usery = new UserY();
			usery.setZname(name.getMname());
			usery.setMoney(name.getMonry());
			integer = userDao.updatejianM(usery);
		}
		return integer;
	}

	@Override
	public Integer updateTxImg(UserY usery) {
		return userDao.updateTxImg(usery);
	}

	@Override
	public UserY findUserByUserId(UserY name) {
		return userDao.findUserByUserId(name);
	}

	public static boolean reg(String str, String flag) {

		String regEx = "";
		if ("email".equals(flag)) {
			regEx = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+";
		} else if ("phone".equals(flag)) {
			regEx = "^1[3|5|7|8]\\d{9}$";
		}

		// 编译正则表达式
		Pattern pattern = Pattern.compile(regEx);
		// 忽略大小写的写法
		// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		// 字符串是否与正则表达式相匹配
		boolean rs = matcher.matches();
		System.out.println(rs);
		return rs;
	}

	@Override
	public Integer updateMyPerson(UserY usery) {
		return userDao.updateMyPerson(usery);
	}

}
